package com.cy.ppj.service.mall.impl;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.mall.GoodsImageMapper;
import com.cy.ppj.dao.mapper.mall.GoodsMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.ao.mall.GoodsAddAO;
import com.cy.ppj.model.ao.mall.GoodsEditAO;
import com.cy.ppj.model.ao.mall.GoodsImageAddAO;
import com.cy.ppj.model.domian.mall.Goods;
import com.cy.ppj.model.domian.mall.GoodsImage;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.enums.mall.GoodsImageType;
import com.cy.ppj.model.query.mall.GoodsAdminListQuery;
import com.cy.ppj.model.vo.mall.GoodsAdminListVO;
import com.cy.ppj.model.vo.mall.GoodsAdminDetailVO;
import com.cy.ppj.model.vo.mall.GoodsImageListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.mall.GoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 商品 service
 * @Author tanyilin
 * @Date 2019/7/3 15:54
 */
@Service
public class GoodsServiceImpl extends BaseService implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private GoodsImageMapper goodsImageMapper;
    @Autowired
    private FileService fileService;

    @Override
    public PageBean<GoodsAdminListVO> getGoodsAdminList(GoodsAdminListQuery query) {

        int total = goodsMapper.selectGoodsAdminListCount(query);
        List<GoodsAdminListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = goodsMapper.selectGoodsAdminList(query);
            list.stream().forEach(g -> {
                g.setStatusText(g.getStatus().getText2());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    @Transactional
    public void updateGoods(Goods updateGoods) {

        updateGoods.setUpdateTime(new Date());
        goodsMapper.updateByPrimaryKeySelective(updateGoods);
    }

    @Override
    public GoodsAdminDetailVO getGoodsAdminDetail(Long goodsId) {

        Goods dbGoods = goodsMapper.selectByPrimaryKey(goodsId);
        state(dbGoods != null, "查询商品详情数据异常");

        GoodsAdminDetailVO detailVO = new GoodsAdminDetailVO();
        BeanUtils.copyProperties(dbGoods, detailVO);
        detailVO.setGoodsId(dbGoods.getId());

        //查询头图数组
        List<GoodsImageListVO> topBannerImageList = goodsImageMapper.selectGoodsImageListByGoodsId(goodsId, GoodsImageType.DETAIL_TOP_BANNER_IMAGE);
        topBannerImageList.stream().forEach(i -> {
            i.setSrc(fileService.getFileUrl(i.getPath(), Constants.MALL_BUCKET_NAME));
        });
        //查询介绍图数组
        List<GoodsImageListVO> introductionImageList = goodsImageMapper.selectGoodsImageListByGoodsId(goodsId, GoodsImageType.DETAIL_BOTTOM_IMAGE);
        introductionImageList.stream().forEach(i -> {
            i.setSrc(fileService.getFileUrl(i.getPath(), Constants.MALL_BUCKET_NAME));
        });

        detailVO.setTopBannerImageList(topBannerImageList);
        detailVO.setIntroductionImageList(introductionImageList);
        return detailVO;
    }

    @Override
    @Transactional
    public void addGoods(GoodsAddAO addAO) {

        Date time = new Date();

        //新增商品
        Goods insertGoods = new Goods();
        BeanUtils.copyProperties(addAO, insertGoods);
        insertGoods.setProductId(Constants.JYD_PRODUCTID);
        insertGoods.setTitle(addAO.getName());
        if(CommonUtils.isEmpty(insertGoods.getWeight())) {
            insertGoods.setWeight(goodsMapper.selectMaxWeight() + 1);
        }
        insertGoods.setStatus(CommonStatus.ENABLE);
        insertGoods.setCreateTime(time);
        insertGoods.setUpdateTime(time);
        goodsMapper.insertSelective(insertGoods);
        Long goodsId = insertGoods.getId();

        //保存商品图片
        List<GoodsImageAddAO> topBannerImageList = addAO.getTopBannerImageList();
        //保存display图
        if(!CommonUtils.isEmpty(topBannerImageList)) {
            List<GoodsImage> goodsImageList = new ArrayList<>();
            GoodsImageAddAO sourceBean = topBannerImageList.get(0);
            GoodsImage newGoodsImage = new GoodsImage();
            newGoodsImage.setGoodsId(goodsId);
            newGoodsImage.setType(GoodsImageType.DISPLAY_IMAGE);
            newGoodsImage.setPath(sourceBean.getPath());
            newGoodsImage.setOrder(0);
            newGoodsImage.setCreateTime(time);
            newGoodsImage.setUpdateTime(time);
            goodsImageList.add(newGoodsImage);

            //批量新增
            goodsImageMapper.insertBatch(goodsImageList);
            //删除无效数据
            goodsImageMapper.deleteByUpdateTimeAndType(goodsId, GoodsImageType.DISPLAY_IMAGE, time);

        }
        //保存topBanner图
        if(!CommonUtils.isEmpty(topBannerImageList)) {
            List<GoodsImage> goodsImageList = new ArrayList<>();
            int index = 1;
            for(GoodsImageAddAO sourceBean : topBannerImageList) {
                GoodsImage newGoodsImage = new GoodsImage();
                newGoodsImage.setGoodsId(goodsId);
                newGoodsImage.setType(GoodsImageType.DETAIL_TOP_BANNER_IMAGE);
                newGoodsImage.setPath(sourceBean.getPath());
                newGoodsImage.setOrder(index);
                newGoodsImage.setCreateTime(time);
                newGoodsImage.setUpdateTime(time);
                goodsImageList.add(newGoodsImage);
                index++;
            }
            //批量新增
            goodsImageMapper.insertBatch(goodsImageList);
            //删除无效数据
            goodsImageMapper.deleteByUpdateTimeAndType(goodsId, GoodsImageType.DETAIL_TOP_BANNER_IMAGE, time);
        }

        //保存底部介绍图
        List<GoodsImageAddAO> introductionImageList = addAO.getIntroductionImageList();
        if(!CommonUtils.isEmpty(introductionImageList)) {
            List<GoodsImage> goodsImageList = new ArrayList<>();
            int index = 1;
            for(GoodsImageAddAO sourceBean : introductionImageList) {
                GoodsImage newGoodsImage = new GoodsImage();
                newGoodsImage.setGoodsId(goodsId);
                newGoodsImage.setType(GoodsImageType.DETAIL_BOTTOM_IMAGE);
                newGoodsImage.setPath(sourceBean.getPath());
                newGoodsImage.setOrder(index);
                newGoodsImage.setCreateTime(time);
                newGoodsImage.setUpdateTime(time);
                goodsImageList.add(newGoodsImage);
                index++;
            }
            //批量新增
            goodsImageMapper.insertBatch(goodsImageList);
            //删除无效数据
            goodsImageMapper.deleteByUpdateTimeAndType(goodsId, GoodsImageType.DETAIL_BOTTOM_IMAGE, time);
        }

    }

    @Override
    @Transactional
    public void editGoods(GoodsEditAO editAO) {

        Date time = new Date();
        Long goodsId = editAO.getGoodsId();

        //更新商品信息
        Goods updateGoods = new Goods();
        updateGoods.setId(goodsId);
        BeanUtils.copyProperties(editAO, updateGoods);
        updateGoods.setTitle(editAO.getName());
        goodsMapper.updateByPrimaryKeySelective(updateGoods);

        //保存商品图片
        List<GoodsImageAddAO> topBannerImageList = editAO.getTopBannerImageList();
        //保存display图
        if(!CommonUtils.isEmpty(topBannerImageList)) {
            List<GoodsImage> goodsImageList = new ArrayList<>();
            GoodsImageAddAO sourceBean = topBannerImageList.get(0);
            GoodsImage newGoodsImage = new GoodsImage();
            newGoodsImage.setGoodsId(goodsId);
            newGoodsImage.setType(GoodsImageType.DISPLAY_IMAGE);
            newGoodsImage.setPath(sourceBean.getPath());
            newGoodsImage.setOrder(0);
            newGoodsImage.setCreateTime(time);
            newGoodsImage.setUpdateTime(time);
            goodsImageList.add(newGoodsImage);

            //批量新增
            goodsImageMapper.insertBatch(goodsImageList);
            //删除无效数据
            goodsImageMapper.deleteByUpdateTimeAndType(goodsId, GoodsImageType.DISPLAY_IMAGE, time);

        }
        //保存topBanner图
        if(!CommonUtils.isEmpty(topBannerImageList)) {
            List<GoodsImage> goodsImageList = new ArrayList<>();
            int index = 1;
            for(GoodsImageAddAO sourceBean : topBannerImageList) {
                GoodsImage newGoodsImage = new GoodsImage();
                newGoodsImage.setGoodsId(goodsId);
                newGoodsImage.setType(GoodsImageType.DETAIL_TOP_BANNER_IMAGE);
                newGoodsImage.setPath(sourceBean.getPath());
                newGoodsImage.setOrder(index);
                newGoodsImage.setCreateTime(time);
                newGoodsImage.setUpdateTime(time);
                goodsImageList.add(newGoodsImage);
                index++;
            }
            //批量新增
            goodsImageMapper.insertBatch(goodsImageList);
            //删除无效数据
            goodsImageMapper.deleteByUpdateTimeAndType(goodsId, GoodsImageType.DETAIL_TOP_BANNER_IMAGE, time);
        }

        //保存底部介绍图
        List<GoodsImageAddAO> introductionImageList = editAO.getIntroductionImageList();
        if(!CommonUtils.isEmpty(introductionImageList)) {
            List<GoodsImage> goodsImageList = new ArrayList<>();
            int index = 1;
            for(GoodsImageAddAO sourceBean : introductionImageList) {
                GoodsImage newGoodsImage = new GoodsImage();
                newGoodsImage.setGoodsId(goodsId);
                newGoodsImage.setType(GoodsImageType.DETAIL_BOTTOM_IMAGE);
                newGoodsImage.setPath(sourceBean.getPath());
                newGoodsImage.setOrder(index);
                newGoodsImage.setCreateTime(time);
                newGoodsImage.setUpdateTime(time);
                goodsImageList.add(newGoodsImage);
                index++;
            }
            //批量新增
            goodsImageMapper.insertBatch(goodsImageList);
            //删除无效数据
            goodsImageMapper.deleteByUpdateTimeAndType(goodsId, GoodsImageType.DETAIL_BOTTOM_IMAGE, time);
        }

    }
}
